package com.wuze.cloud.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;

/**
 * @author: wuze
 * @description:
 * @date: 2023/02/18
 */
public class PageRequest implements Serializable {

    private static final long serialVersionUID = -7704947701491349330L;
    private Page page = new Page(15, 1);

    public PageRequest() {
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    @JsonIgnore
    public int getOffset() {
        if (page == null) {
            return 0;
        }
        int tmp = (page.pageNo - 1) * page.pageSize;
        return tmp < 0 ? 0 : tmp;
    }

    @JsonIgnore
    public int getLimit() {
        if (page == null) {
            return 0;
        }
        return page.pageSize;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof PageRequest)) {
            return false;
        }

        PageRequest that = (PageRequest) o;

        return page != null ? page.equals(that.page) : that.page == null;
    }

    @Override
    public int hashCode() {
        return page != null ? page.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "PageRequest{" +
                "page=" + page +
                '}';
    }

    @Data
    public static class Page implements Serializable {

        private static final long serialVersionUID = -9116229816861557536L;

        int pageSize = 15;

        int pageNo = 1;

        int limit = 0;

        public Page() {
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.limit = (this.pageNo - 1) * pageSize;
            this.pageSize = pageSize;
        }

        public int getPageNo() {
            return pageNo;
        }

        public void setPageNo(int pageNo) {
            this.limit = (pageNo - 1) * this.pageSize;
            this.pageNo = pageNo;
        }

        public Page(int pageSize, int pageNo) {
            this.pageSize = pageSize;
            this.pageNo = pageNo;
            this.limit = (pageNo - 1) * pageSize;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (!(o instanceof Page)) {
                return false;
            }
            Page page = (Page) o;
            if (pageSize != page.pageSize) {
                return false;
            }
            if (pageNo != page.pageNo) {
                return false;
            }
            return true;
        }

        @Override
        public int hashCode() {
            int result = pageSize;
            result = 31 * result + pageNo;
            return result;
        }

        @Override
        public String toString() {
            return "Page{" +
                    "pageSize=" + pageSize +
                    ", pageNo=" + pageNo +
                    ", limit=" + limit +
                    '}';
        }
    }
}

